#!/usr/bin/env python

def permutation(nums, k):
    '''
    给出从数组nums中取出k个值的排列
    '''
    def _backtracking(nums, result, k, visted, alist):
        if len(alist) == k:
            result.append(alist[:])
            return
        
        for num in nums:
            if not visted[num]:
                visted[num] = True
                alist.append(num)
                _backtracking(nums, result, k, visted, alist)
                alist.pop()
                visted[num] = False
    
    ans = []
    visted = {num : False for num in nums}
    _backtracking(nums, ans, k, visted, [])
    return ans


if __name__ == "__main__":
    print(permutation([i for i in range(1, 5)], 3))
